package com.topnav.mc;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.js.lib.utils.DateUtils;
import com.topnav.mc.listener.bean.GPSMsgInfo;
import com.topnav.mc.listener.bean.YLDeviceInfo;
import com.topnav.mc.uitls.FastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: kenny
 * @Date: 2023/12/20 10:11
 */
//@RunWith(SpringRunner.class) //加这句才能跑系统的环境
@SpringBootTest
@Slf4j
public class MobilePositioinText {

    //@Test
    public void sendPosition(){
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration("127.0.0.1", 16379);
        configuration.setDatabase(2);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(configuration);
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        // 使用fastJson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.afterPropertiesSet();
        List<GPSMsgInfo> list = new ArrayList<>();

        list.add(new GPSMsgInfo().setSerial("44000000491327000001").setCode("44000000491327000001").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491327000003").setCode("44030000491327000003").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44010200491320000050").setCode("44010200491320000050").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44010200492000000006").setCode("44010200492000000006").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44010200492000000020").setCode("44030000492000000020").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491327000002").setCode("44030000491327000002").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491327000004").setCode("44030000491327000004").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491327000001").setCode("44030000491327000001").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491327000005").setCode("44030000491327000005").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("34020000001320000008").setCode("34020000001320000001").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));
		list.add(new GPSMsgInfo().setSerial("44030000491135000054").setCode("44030000491135000054").setAltitude("0").setLongitude(114.20260716845759).setLatitude(114.20260716845759).setDirection("0").setSpeed(0));

        List<YLDeviceInfo> deviceInfoslist = new ArrayList<>();
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44000000491327000001").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491327000003").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44010200491320000050").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44010200492000000020").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491327000002").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491327000004").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491327000001").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491327000005").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("34020000001320000008").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));
        deviceInfoslist.add(new YLDeviceInfo().setDeviceId("44030000491135000054").setPoliceNumber("").setBatteryInfo("0").setStorage("0").setUsedMemory("0").setRemainMemory("0").setRecordStatus("0"));

        List<JSONObject> keepalivelist = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();jsonObject.put("deviceId","44000000491327000001");
        JSONObject jsonObject1 = new JSONObject();jsonObject1.put("deviceId","44030000491327000003");
        JSONObject jsonObject2 = new JSONObject();jsonObject2.put("deviceId","44010200491320000050");
        JSONObject jsonObject3 = new JSONObject();jsonObject3.put("deviceId","44010200492000000020");
        JSONObject jsonObject4 = new JSONObject();jsonObject4.put("deviceId","44030000491327000002");
        JSONObject jsonObject5 = new JSONObject();jsonObject5.put("deviceId","44030000491327000004");
        JSONObject jsonObject6 = new JSONObject();jsonObject6.put("deviceId","44030000491327000001");
        JSONObject jsonObject7 = new JSONObject();jsonObject7.put("deviceId","44030000491327000005");
        JSONObject jsonObject8 = new JSONObject();jsonObject8.put("deviceId","34020000001320000008");
        JSONObject jsonObject9 = new JSONObject();jsonObject9.put("deviceId","44030000491135000054");

        keepalivelist.add(jsonObject);
        keepalivelist.add(jsonObject1);
        keepalivelist.add(jsonObject2);
        keepalivelist.add(jsonObject3);
        keepalivelist.add(jsonObject4);
        keepalivelist.add(jsonObject5);
        keepalivelist.add(jsonObject6);
        keepalivelist.add(jsonObject7);
        keepalivelist.add(jsonObject8);
        keepalivelist.add(jsonObject9);
		try {
            List<Thread> threadList = new ArrayList<Thread>();
            Thread mpThread = new Thread(new MpRunnable("Mp",list,2000,redisTemplate));
            mpThread.start();
            threadList.add(mpThread);
            Thread diThread = new Thread(new DiRunnable("Di",deviceInfoslist,2000,redisTemplate));
            diThread.start();
            threadList.add(diThread);
            Thread kaThread = new Thread(new KaRunnable("Ka",keepalivelist,2000,redisTemplate));
            kaThread.start();
            threadList.add(kaThread);

            log.info(">>>>>>>>end");

            for(Thread tr : threadList) {
                tr.join();
            };
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.toString());
        }
    }

    static class DiRunnable implements Runnable{
        private String code=null;
        private List<YLDeviceInfo> list=null;
        private RedisTemplate redisTemplate=null;
        private int count=0;
        public DiRunnable(String code,List<YLDeviceInfo> list,int count,RedisTemplate redisTemplate){
            this.code = code;
            this.list = list;
            this.count = count;
            this.redisTemplate = redisTemplate;
        }
        public void run(){
            for (int i=0;i<count;i++){
                for(YLDeviceInfo ylDeviceInfo : list){
                    redisTemplate.convertAndSend("YLDeviceInfo", JSON.toJSON(ylDeviceInfo));
                    log.info("{}-{}-{}:{}",code,Thread.currentThread().getName(),i,ylDeviceInfo.getDeviceId() );
                    try {
                        Thread.sleep(2L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class MpRunnable implements Runnable{
        private String code=null;
        private List<GPSMsgInfo> list=null;
        private RedisTemplate redisTemplate=null;
        private int count=0;
        public MpRunnable(String code,List<GPSMsgInfo> list,int count,RedisTemplate redisTemplate){
            this.code = code;
            this.list = list;
            this.count = count;
            this.redisTemplate = redisTemplate;
        }
        public void run(){
            for (int i=0;i<count;i++){
                for(GPSMsgInfo gpsMsgInfo : list){
                    gpsMsgInfo.setTime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    //System.out.println(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS"));
                    redisTemplate.convertAndSend("mobileposition", JSON.toJSON(gpsMsgInfo));
                    //System.out.println(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS"));
                    //redisTemplate.convertAndSend("mobileposition1", gpsMsgInfo);
                    //System.out.println(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS"));
                    //redisTemplate.opsForValue().set("mobileposition-" + gpsMsgInfo.getSerial(), gpsMsgInfo, 30, TimeUnit.SECONDS);
                    log.info("{}-{}-{}:{}",code,Thread.currentThread().getName(),i,gpsMsgInfo.getSerial() );
                    try {
                        Thread.sleep(2L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class KaRunnable implements Runnable{
        private String code=null;
        private List<JSONObject> list=null;
        private RedisTemplate redisTemplate=null;
        private int count=0;
        public KaRunnable(String code,List<JSONObject> list,int count,RedisTemplate redisTemplate){
            this.code = code;
            this.list = list;
            this.count = count;
            this.redisTemplate = redisTemplate;
        }
        public void run(){
            for (int i=0;i<count;i++){
                for(JSONObject jSONObject : list){
                    redisTemplate.convertAndSend("DEVICE-KEEPALIVE", jSONObject);
                    log.info("{}-{}-{}:{}",code,Thread.currentThread().getName(),i,jSONObject.get("deviceId") );
                    try {
                        Thread.sleep(2L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
